    //
//  RootVC_iPad.m
//  WRY
//
//  Created by Yang on 10/12/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "RootVC_iPad.h"
#import "IndexViewController.h"
#import "PopCantonVC_iPad.h"
#import "PopVocationVC_iPad.h"
#import "PopCategoryVC_iPad.h"
#import "AppDelegate_iPad.h"
#import "SearchVC_iPad.h"
#import "StaticVariable.h"
#import "SettingListVc.h"
#import "NSStringExtra.h"

extern NSString *namecode;
extern NSString *nameStr;


@implementation RootVC_iPad
@synthesize welcomeLabel,iconButton,menuShowLabel,rootSearchBar;
@synthesize popController,indexViewController,popCantonVc,popVoctionVc,popCategoryVc;
@synthesize enterListVC,searchVC,companydetailVc,focusListVc;
@synthesize popSettingVc;
#pragma mark -
#pragma mark 初始化窗体
-(void)initWindow
{
    NSString *loginUserName=[[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
	if ([loginUserName isEqualToString:@"chenmm"]) {
		welcomeLabel.text=@"热烈欢迎江苏省环保厅陈蒙蒙厅长登录系统";
	}
	else {
		welcomeLabel.text=[NSString stringWithFormat:@"欢迎使用%@污染源在线监控平台",[[AppDelegate_iPad currentDelegate] showCantonName]];
	}
	self.menuShowLabel.text=@"首页";
	menuShowLabel.textColor=[UIColor whiteColor];
	menuShowLabel.textAlignment=UITextAlignmentCenter;
	rootSearchBar.delegate=self;
    rootSearchBar.searchBarStyle = UISearchBarStyleMinimal;
//	[[rootSearchBar.subviews objectAtIndex:0]removeFromSuperview];
}
#pragma mark -
#pragma mark 设置webservice路径按钮
-(IBAction)settWetPathClick:(id)sender
{
	if (self.popSettingVc==nil) {
		SettingListVc *setVc=[[SettingListVc alloc]init];
		[setVc setContentSizeForViewInPopover:CGSizeMake(280,300)];
		self.popSettingVc=setVc;
        self.popSettingVc.rootVc=self;
		[setVc release];
	}
	UINavigationController *navi=[[UINavigationController alloc]
								  initWithRootViewController:self.popSettingVc];
	UIPopoverController *popVc=[[UIPopoverController alloc]initWithContentViewController:navi];
	[navi release];
	popVc.delegate=self;
	self.popController=popVc;
	[popVc release];
	UIButton *btn=(UIButton *)sender;
	[self.popController presentPopoverFromRect:btn.bounds inView:btn permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];	

}
- (void)viewDidLoad {
    [super viewDidLoad];
    //全局变量
	namecode=[[AppDelegate_iPad currentDelegate] userCantonCodes];
	nameStr=[[AppDelegate_iPad currentDelegate] showCantonName];
	[self initWindow];
	[self loadIndexController];

}
-(void)viewDidAppear:(BOOL)animated{
    if ([[[UIDevice currentDevice]systemVersion]floatValue]>6.1) {
        CGRect rect = self.view.frame;
        rect.origin.y = rect.origin.y + 20;
        rect.size.height = rect.size.height - 20;
        self.view.frame = rect;

        [self.view bringSubviewToFront:rootSearchBar];
        [rootSearchBar setBackgroundColor:[UIColor clearColor]];
        rootSearchBar.translucent = NO;
        


    }
}
#pragma mark -
#pragma mark 加载首页ViewController
-(void)loadIndexController
{
	if (self.indexViewController==nil)
	{
        IndexViewController *indexVc=[[IndexViewController alloc]initWithNibName:@"IndexViewController" bundle:nil];
		self.indexViewController=indexVc;
        [indexVc release];
		self.indexViewController.rootVc=self;
		[self.view addSubview:self.indexViewController.view];
	}
	//首页按钮的button
	UIButton *btn=(UIButton *)[self.view viewWithTag:1];
	btn.selected=YES;
}
#pragma mark -
#pragma mark Button 点击
#pragma mark -搜索
-(IBAction)searchBtnClick:(id)sender
{
    [rootSearchBar resignFirstResponder];
	if (self.searchVC!=nil) {
		[self.searchVC.view removeFromSuperview];
	}
	NSString *searchStr=[self.rootSearchBar text];//搜索条件
	SearchVC_iPad *searchV=[[SearchVC_iPad alloc]init];
	//添加关闭按钮
	UIButton *closeButton=[[UIButton alloc]initWithFrame:CGRectMake(280, -5, 29, 27)];
	[closeButton setImage:[UIImage imageNamed:@"cha.png"] forState:UIControlStateNormal];
	[closeButton addTarget:self action:@selector(closeSearchAction:) 
		  forControlEvents:UIControlEventTouchUpInside];
	[searchV.view addSubview:closeButton];
	[closeButton release];
	searchV.keyword=searchStr;
	searchV.rootVc=self;
	self.searchVC=searchV;
	[searchV release];
	[self.view addSubview:self.searchVC.view];
	[self.searchVC linkData];
}
-(void)closeSearchAction:(id)sender
{
	rootSearchBar.text=@"";
	if (searchVC!=nil) {
		[self.searchVC.view removeFromSuperview];
	}
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
	[self searchBtnClick:searchBar];
}
//改变菜单按钮的背景
-(void)changebtnBack
{
	for(int i=1;i<=5;i++)
	{
		UIButton *btn=(UIButton *)[self.view viewWithTag:i];
		btn.selected=NO;
	}
}
#pragma mark -
#pragma mark 菜单栏按钮点击事件
-(IBAction)topButtonClick:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	int tag=btn.tag;
	[self changebtnBack];
	btn.selected=YES;
	switch (tag) {
		case 1:
			[iconButton setImage:[UIImage imageNamed:@"首页.png"] 
								  forState:UIControlStateNormal];
			self.menuShowLabel.text=@"首页";
			[self performSelector:@selector(indexPageSelectMethod)];
			break;
		case 2:
			[iconButton setImage:[UIImage imageNamed:@"btn30X30area.png"] 
								  forState:UIControlStateNormal];
			self.menuShowLabel.text=@"地域板块";
			[self performSelector:@selector(areaSelectMethod:) withObject:sender];
			break;
		case 3:
			[iconButton setImage:[UIImage imageNamed:@"btn30X30hangye.png"] 
								  forState:UIControlStateNormal];
			self.menuShowLabel.text=@"行业类别";
			[self performSelector:@selector(areaSelectMethod:) withObject:sender];
			break;
		case 4:
			[iconButton setImage:[UIImage imageNamed:@"btn30X30jiankong.png"] 
								  forState:UIControlStateNormal];
			self.menuShowLabel.text=@"关注类型";
			[self performSelector:@selector(jiankongSelectMethod:) withObject:sender];
			break;
		case 5:
			[iconButton setImage:[UIImage imageNamed:@"btn30X30attention.png"] 
								  forState:UIControlStateNormal];
			self.menuShowLabel.text=@"关注企业";
			[self performSelector:@selector(attentionSelectMethod)];
			break;
		default:
			break;
	}	
}
#pragma mark -
#pragma mark 首页事件
-(void)removeModelView
{
	[self.indexViewController.view setHidden:YES];
	[self.enterListVC.view setHidden:YES];
	[self.searchVC.view setHidden:YES];
	[self.companydetailVc.view setHidden:YES];
	[self.focusListVc.view setHidden:YES];
}
//首页选中方法
-(void)indexPageSelectMethod
{
	[self removeModelView];
	self.indexViewController.view.hidden = NO;
	[self.view bringSubviewToFront:self.indexViewController.view];
}
#pragma mark 选择行政区，和行业类别
-(void)areaSelectMethod:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	UINavigationController *navi=[[UINavigationController alloc]init];
	if (btn.tag==2)
	{
		//行政区
		if (self.popCantonVc ==nil) 
		{
			PopCantonVC_iPad *cantonController=[[PopCantonVC_iPad alloc]init];
			self.popCantonVc=cantonController;
			[cantonController release];
			popCantonVc.contentSizeForViewInPopover=CGSizeMake(320, 400.0f);
//            NSLog(@"%@",NSStringFromCGRect(popCantonVc.view.frame));
			popCantonVc.rootVC=self;
		}
		[navi initWithRootViewController:popCantonVc];
	}
	if (btn.tag==3)
	{
		//地域板块
		if (self.popVoctionVc ==nil) 
		{
			PopVocationVC_iPad *vocController=[[PopVocationVC_iPad alloc]init];
			self.popVoctionVc=vocController;
			[vocController release];
			popVoctionVc.contentSizeForViewInPopover=CGSizeMake(240, 320.0f);
			popVoctionVc.rootVC=self;
			
		}
		
		[navi initWithRootViewController:popVoctionVc];
	}
	UIPopoverController *pop=[[UIPopoverController alloc]initWithContentViewController:navi];
	[navi release];
	pop.delegate=self;
	self.popController=pop;
	[pop release];
	[self.popController presentPopoverFromRect:btn.bounds inView:btn 
					  permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
		
}
#pragma mark 监控分类
-(void)jiankongSelectMethod:(id)sender
{
	UIButton *btn=(UIButton *)sender;
	if (self.popCategoryVc==nil) {
		PopCategoryVC_iPad *categoryVc=[[PopCategoryVC_iPad alloc]init];
		self.popCategoryVc=categoryVc;
		[categoryVc release];
		popCategoryVc.contentSizeForViewInPopover=CGSizeMake(240.0f, 320.0f);
		popCategoryVc.rootVc=self;
	}
	UINavigationController *navi=[[UINavigationController alloc]initWithRootViewController:self.popCategoryVc];
	UIPopoverController *pop=[[UIPopoverController alloc]initWithContentViewController:navi];
	[navi release];
	pop.delegate=self;
	self.popController=pop;
	[pop release];
	[self.popController presentPopoverFromRect:btn.bounds inView:btn 
					  permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
}
#pragma mark 关注企业
-(void)attentionSelectMethod
{
	[self removeModelView];
	if (self.focusListVc==nil) {
        
        CompanyDetaiVC_iPad *focusVc=[[CompanyDetaiVC_iPad alloc]init];
		[focusVc.view setFrame:CGRectMake(0, 100, 1024, 648)];
        self.focusListVc=focusVc;
        [focusVc release];
		[self.view addSubview:self.focusListVc.view];
		[focusListVc setForFocusDetail];
	}
	focusListVc.detaiVCdelegate = self;
	focusListVc.view.hidden = NO;
	[self.view bringSubviewToFront:focusListVc.view];
	
	
}
#pragma mark 污染源列表，CoverFlow
-(void)addEnterpriseVC:(NSInteger)fromType enterType:(NSString *)enterpriseType
{
	[self removeModelView];
	if (self.enterListVC==nil) {
        EnterpriseListVC_iPad *listVc=[[EnterpriseListVC_iPad alloc]init];
        self.enterListVC=listVc;
        [listVc release];
        self.enterListVC.wrydetailTypeCode=fromType;
		self.enterListVC.rootVc=self;
		[self.view addSubview:self.enterListVC.view];
	}
	self.enterListVC.delegate=self;
	self.enterListVC.view.hidden = NO;
	[self.view bringSubviewToFront:self.enterListVC.view];
	[self.enterListVC reloadPageEntList:fromType enterType:enterpriseType];
	NSString *menuString=[NSString stringWithFormat:@""];
	if (fromType==1000) {
		menuString=[NSString stringWithFormat:@"地域板块·%@",nameStr];
	}
	else if(fromType==2000)
	{
		menuString=[NSString stringWithFormat:@"行业类别·%@",nameStr];
	}
	else {
		menuString=[NSString stringWithFormat:@"关注类型·%@",nameStr];
	}

	self.menuShowLabel.text=menuString;
}
#pragma mark 企业详情
-(void)addcompanydeatilVC:(NSString *)companyCode companyTypeCode:(NSString *)comTypeCode
                 siteCode:(NSString *)siteCode 
			 sitetypeCode:(NSString *)sitetypeCode factoryCode:(NSString *)factoryCode
{
	[self removeModelView];
	if (self.companydetailVc==nil) {
        CompanyDetaiVC_iPad *comDetailVc=[[CompanyDetaiVC_iPad alloc]init];
		self.companydetailVc=comDetailVc;
        [comDetailVc release];
		[self.companydetailVc.view setFrame:CGRectMake(0, 100, 1024, 648)];
		[self.view addSubview:self.companydetailVc.view];
	}
	companydetailVc.detaiVCdelegate=self;
	companydetailVc.view.hidden = NO;
	[self.view bringSubviewToFront:companydetailVc.view];
    if (siteCode==nil || [siteCode isEmptyOrNull]) {
        siteCode=@"";
    }
    if (sitetypeCode==nil || [sitetypeCode isEmptyOrNull]) {
        sitetypeCode=@"";
    }
    if (factoryCode==nil || [factoryCode isEmptyOrNull]) {
        factoryCode=@"";
    }
	companydetailVc.marksiteCode=siteCode;
	companydetailVc.markfactoryCode=factoryCode;
	companydetailVc.marksitetypeCode=sitetypeCode;
	companydetailVc.markEnterpriseCode=companyCode;
    companydetailVc.markEnterpriseTypeCode=comTypeCode;
	
	
	[companydetailVc beginRequestForCompany:companyCode companyTypeCode:comTypeCode];
	

}
#pragma mark 返回上一级事件
-(IBAction)backPage:(id)sender
{
	[self removeModelView];
	if ([menuShowLabel.text isEqualToString:@"首页"]) {
		[self.indexViewController.view setHidden:NO];
		[self.view bringSubviewToFront:indexViewController.view];
	}
	else if([menuShowLabel.text isEqualToString:@"关注企业"]) {
		
		[self.focusListVc.view setHidden:NO];
		[self.view bringSubviewToFront:focusListVc.view];
	}
	else {
		[self.enterListVC.view setHidden:NO];
		[self.view bringSubviewToFront:enterListVC.view];
	}

}
#pragma mark UIPopoverControllerDelegate 
- (BOOL)popoverControllerShouldDismissPopover:(UIPopoverController *)popoverController {
	return NO;
}
- (void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController {
	[popoverController release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  
	return (interfaceOrientation==UIInterfaceOrientationLandscapeLeft || interfaceOrientation==UIInterfaceOrientationLandscapeRight);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
	welcomeLabel=nil;
   	iconButton=nil;
	menuShowLabel=nil;
	rootSearchBar=nil;
}


- (void)dealloc {
	[popSettingVc release];
	[focusListVc release];
	[companydetailVc release];
	[popCategoryVc release];
	[searchVC release];
	[popVoctionVc release];
	[enterListVC release];
	[popCantonVc release];
	[indexViewController release];
	[rootSearchBar release];
	[iconButton release];
	[menuShowLabel release];
	[welcomeLabel release];
	[super dealloc];
}
#pragma mark 在企业列地图点击到企业详情页面
- (void)mapbuttonPressedInEnterlist:(NSDictionary *)proporties
{
	
    if (proporties !=nil && [[proporties allKeys]count]>4) {
        [self addcompanydeatilVC:[proporties objectForKey:@"EnterpriseCode"] 
                        companyTypeCode:[proporties objectForKey:@"EnterpriseType"]  
                        siteCode:@""  
                    sitetypeCode:@""
                     factoryCode:@""];
    }
	
}
-(void)naviBigMapVc:(NSDictionary *)proporties
{
	BigMapVC *controller = [[BigMapVC alloc] initWithNibName:@"BigMapVC" bundle:[NSBundle mainBundle]];
	controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
	controller.modalPresentationStyle = UIModalPresentationFullScreen;
    controller.dataDict = [[NSMutableDictionary alloc]initWithDictionary:proporties];
	UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:controller];
	
	[self presentModalViewController:navi animated:YES];
   
    [controller release];
	[navi release];
	
}
#pragma mark CompanyDetaiVC_iPadDelagete
- (void)mapbuttonPressedInDetailPage:(NSDictionary *)proporties {
	[self naviBigMapVc:proporties];
}
- (void)mapbuttonPressedNavi:(NSDictionary *)proporties
{
	[self naviBigMapVc:proporties];
}

@end
